package com.victualler.core.config.menu.repository.impl;

import com.victualler.core.base.repository.impl.BaseQueryRepositoryImpl;
import com.victualler.core.common.dto.PageDto;
import com.victualler.core.common.utils.ExceptionWapperUtil;
import com.victualler.core.common.utils.HQLConverterUtil;
import com.victualler.core.config.menu.dto.MenuDto;
import com.victualler.core.config.menu.dto.request.MenuRequestDto;
import com.victualler.core.config.menu.dto.response.MenuResponseDto;
import com.victualler.core.config.menu.entity.MenuDao;
import com.victualler.core.config.menu.repository.MenuQueryRepository;
import com.victualler.core.config.permission.entity.PermissionInfoDao;
import com.victualler.core.config.permission.entity.RolePermissionInfoDao;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Guoshichang
 * @function 封装用于复杂sql查询的API. 为每个实体的repository层提供便利操作
 */
@Component
public class MenuQueryRepositoryImpl extends BaseQueryRepositoryImpl<MenuDao,Long> implements MenuQueryRepository {

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param menuRequestDto
     * @Function  根据条件入参PermissionRequestDto动态分页查询权限
     */
    public PageDto<MenuResponseDto> getByRequestDto(MenuRequestDto menuRequestDto, int from , int limit){
        return this.queryPageByRequestDto(menuRequestDto,MenuResponseDto.class,from,limit);
    }

    /**
     * @author Guoshichang
     * @since 19:39 on 2018/9/15.
     * @return
     * @Function 查询所有
     */
    public List<MenuDto> getAll() {
        String menuDaoTableName = this.getTableName(MenuDao.class);
        String permissionTableName = this.getTableName(PermissionInfoDao.class);
        String sql = "select menu.*,perm.url href from "+menuDaoTableName+" menu left join "+permissionTableName+" perm on perm.id = menu.permission_Id";
        try {
            return HQLConverterUtil.findVOListBySql(sql,new Object[]{},MenuDto.class,entityManager);
        } catch (Exception e) {
            e.printStackTrace();
            throw ExceptionWapperUtil.wapper(e);
        }
    }

    /**
     * @author Guoshichang
     * @since 14:48 on 2018/7/6.
     * @param roleIds
     * @Function  获得某些角色下对应的菜单,(根据 角色对应的权限查询)
     */
    @Override
    public List<MenuDto> getMenuListByRoles(List<Long> roleIds) {
        String menuDaoTableName = this.getTableName(MenuDao.class);
        String rolePermissionTableName = this.getTableName(RolePermissionInfoDao.class);
        String permissionTableName = this.getTableName(PermissionInfoDao.class);
        String sql = "select menu.*,perm.url "+menuDaoTableName+" menu left join "+permissionTableName+" perm on perm.id = menu.permission_Id";
        sql += "where menu.permission_Id in (select permission_Id from "+rolePermissionTableName+" where role_Id in ?)";
        try {
            return HQLConverterUtil.findVOListBySql(sql,new Object[]{},MenuDto.class,entityManager);
        } catch (Exception e) {
            e.printStackTrace();
            throw ExceptionWapperUtil.wapper(e);
        }
    }
}
